package com.milhouz.bomberrokon;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;

public class MainMenuActivity extends Activity {
	private View mStartButton;
	private View mOptionsButton;
	private View mTicker;
	private Animation mAlternateFadeOutAnimation;

	private View.OnClickListener sStartButtonListener = new View.OnClickListener() {
		public void onClick(View v) {
			Intent i = new Intent(getBaseContext(), MapSelectionActivity.class);
			v.startAnimation(mAlternateFadeOutAnimation);
			mAlternateFadeOutAnimation.setAnimationListener(new StartActivityAfterAnimation(i));
			mOptionsButton.startAnimation(mAlternateFadeOutAnimation);
			mTicker.startAnimation(mAlternateFadeOutAnimation);
		}
	};

	private View.OnClickListener sOptionButtonListener = new View.OnClickListener() {
		public void onClick(View v) {
			Intent i = new Intent(getBaseContext(), SetPreferencesActivity.class);
			v.startAnimation(mAlternateFadeOutAnimation);
			mAlternateFadeOutAnimation.setAnimationListener(new StartActivityAfterAnimation(i));
			mStartButton.startAnimation(mAlternateFadeOutAnimation);
			mTicker.startAnimation(mAlternateFadeOutAnimation);
		}
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// Set the fullscreen mode
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

		setContentView(R.layout.main_menu);

		mStartButton = findViewById(R.id.startButton);
		mOptionsButton = findViewById(R.id.optionButton);

		if (mStartButton != null) {
			mStartButton.setOnClickListener(sStartButtonListener);
		}

		if (mOptionsButton != null) {
			mOptionsButton.setOnClickListener(sOptionButtonListener);
		}

		mAlternateFadeOutAnimation = AnimationUtils.loadAnimation(this, R.anim.fade_out);

		mTicker = findViewById(R.id.ticker);
		if (mTicker != null) {
			mTicker.setFocusable(true);
			mTicker.requestFocus();
			mTicker.setSelected(true);
		}

	}

	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {
		// TODO What to do when user click BACK or MENU
		// if (keyCode != KeyEvent.KEYCODE_MENU || keyCode != KeyEvent.KEYCODE_BACK) {
		// return true;
		// }
		return true;
	}

	@Override
	protected void onResume() {
		super.onResume();

		if (mStartButton != null) {
			mStartButton.setVisibility(View.VISIBLE);
			mStartButton.clearAnimation();
			mStartButton.startAnimation(AnimationUtils.loadAnimation(this, R.anim.button_slide));
		}

		if (mOptionsButton != null) {
			mOptionsButton.setVisibility(View.VISIBLE);
			mOptionsButton.clearAnimation();
			Animation anim = AnimationUtils.loadAnimation(this, R.anim.button_slide);
			anim.setStartOffset(200L);
			mOptionsButton.startAnimation(anim);
		}

		if (mTicker != null) {
			mTicker.clearAnimation();
		}
	}

	protected class StartActivityAfterAnimation implements Animation.AnimationListener {
		private Intent mIntent;

		StartActivityAfterAnimation(Intent intent) {
			mIntent = intent;
		}

		public void onAnimationEnd(Animation animation) {
			mStartButton.setVisibility(View.INVISIBLE);
			mStartButton.clearAnimation();
			mOptionsButton.setVisibility(View.INVISIBLE);
			mOptionsButton.clearAnimation();
			startActivity(mIntent);
		}

		public void onAnimationRepeat(Animation animation) {

		}

		public void onAnimationStart(Animation animation) {

		}

	}
}
